package com.coder4j.auth.controller;

import com.coder4j.auth.entity.AuthOrganizationEntity;
import com.coder4j.auth.service.AuthOrganizationService;
import com.coder4j.auth.vo.AuthOrganizationQuery;
import com.coder4j.auth.vo.AuthOrganizationTreeVO;
import com.coder4j.auth.vo.AuthOrganizationVO;
import com.coder4j.auth.vo.AuthOrganizationEO;
import com.coder4j.cache.OrganizationCache;
import com.coder4j.starter.web.api.ApiNote;
import com.coder4j.starter.mybatis.BasePage;
import com.coder4j.starter.tools.vo.R;
import com.coder4j.starter.web.util.EasyExcelUtils;
import com.coder4j.starter.tools.util.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 组织
 * @author keller
 * @date 2024-05-09 11:08:09
 */
@Slf4j
@RestController
@RequestMapping("/authOrganization")
public class AuthOrganizationController {
    @Resource
    private AuthOrganizationService service;

    @Resource
    private HttpServletResponse response;

    /**
     * 按ID查询组织
     * @param id AuthOrganization.id
     * @return 组织对象
     */
    @GetMapping("/getById")
    @ApiNote(name="按ID查询组织",createName = "keller",createTime = "2024-05-09 11:08:09")
    public R<AuthOrganizationVO> getById( Long id){

        return R.success(service.baseGetVOById(id));
    }

    /**
     * 保存组织
     * 有 id 字段，更新；否则新增
     * @param vo AuthOrganization对象
     * @return AuthOrganization对象
     */
    @PostMapping("/save")
    @ApiNote(name="保存组织",createName = "keller",createTime = "2024-05-09 11:08:09")
    public R save(@RequestBody AuthOrganizationVO vo){

        if(ObjectUtils.hasEmpty(vo.getName(),vo.getParentId())){
            return R.paramError();
        }

        if(vo.getSort() == null || vo.getSort() < 0){
            vo.setSort(0);
        }

        service.save(vo);
        OrganizationCache.initCache();
        return R.success();
    }

    /**
     * 查询列表
     * @param query 查询条件
     * @return 数据列表
     */
    @PostMapping("/page")
    @ApiNote(name="查询组织列表",createName = "keller",createTime = "2024-05-09 11:08:09")
    public R<BasePage<AuthOrganizationVO>> getList(@RequestBody AuthOrganizationQuery query){
        if(query == null){
            query = new AuthOrganizationQuery();
        }
        return R.success(service.baseQueryVOPage(query));
    }

    /**
     * 导出列表
     * @param query 查询条件
     */
    @PostMapping("/export")
    @ApiNote(name="导出组织列表",createName = "keller",createTime = "2024-05-09 11:08:09")
    public void export(@RequestBody AuthOrganizationQuery query){
        if(query == null){
            query = new AuthOrganizationQuery();
        }
        List<AuthOrganizationEntity> list = service.baseQueryList(query);
                List<AuthOrganizationEO> eoList = new ArrayList<>();
                for (AuthOrganizationEntity entity : list) {
                    AuthOrganizationEO eo = new AuthOrganizationEO();
                    BeanUtils.copyProperties(entity,eo);
                    eoList.add(eo);
                }
                EasyExcelUtils.exportClass(response,"AuthOrganizationList",eoList);
    }


    /**
     * 删除组织
     * 如果Entity继承了 BaseSoftDeleteEntity，使用逻辑删除；否则，使用物理删除
     * @param id AuthOrganization.id
     * @return 删除的数据
     */
    @PostMapping("/deleteById")
    @ApiNote(name="删除组织",createName = "keller",createTime = "2024-05-09 11:08:09")
    public R deleteById( Long id){
        service.baseDeleteById(id);
        OrganizationCache.initCache();
        return R.success();
    }

    @GetMapping("/getTree")
    @ApiNote(name="获取组织树",createName = "keller",createTime = "2024-05-09 11:08:09")
    public R getTree(){
        return R.success(service.tree());
    }

    @GetMapping("/getTreeSelector")
    @ApiNote(name="获取组织树选择器",createName = "keller",createTime = "2024-05-09 11:08:09")
    public R getTree(Long exclusionId){
        return R.success(service.getTreeSelector(exclusionId));
    }
}
